package org.vartist.atld.util.audio;

import java.util.ArrayList;

import org.vartist.atld.menu.vo.util.audio.VoiceRecord;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.widget.Toast;

public class AudioUtil {

	public static ArrayList<VoiceRecord> getRecordList(Context context) {
		ArrayList<VoiceRecord> list = new ArrayList<VoiceRecord>();
		
		String mTitle;
		String mFilePath;
		String mSize;
		String mDate;
		String mDuration;
		
		String[] projection = new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.MIME_TYPE ,MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATE_ADDED, MediaStore.Audio.Media.DURATION};
		
		ContentResolver cr = context.getContentResolver();
		Cursor cursor = null;
		
		try{
			cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
					MediaStore.Audio.Media.MIME_TYPE + "=? or " + MediaStore.Audio.Media.MIME_TYPE
							+ "=?", new String[] { "audio/amr", "audio/amr-wb" }, null);
			
			if (cursor != null) {
				Toast.makeText(context, "EXTERNAL cursor.getCount() is " + cursor.getCount(), Toast.LENGTH_SHORT).show();
				
				if (cursor.getCount() > 0) {
					cursor.moveToFirst();

					do {
						mTitle = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
						mFilePath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
						mSize = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
						mDate = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED));
						mDuration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

						list.add(new VoiceRecord(context, mTitle, mDate, mDuration, mSize, mFilePath));
					} while (cursor.moveToNext());
				}
				
				cursor.close();
				cursor = null;
				
			} else {
				Toast.makeText(context, "EXTERNAL cursor is null.", Toast.LENGTH_SHORT).show();
			}

			cursor = cr.query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, projection,
					MediaStore.Audio.Media.MIME_TYPE + "=? or " + MediaStore.Audio.Media.MIME_TYPE
							+ "=?", new String[] { "audio/amr", "audio/amr-wb" }, null);
			
			if (cursor != null) {
				Toast.makeText(context, "INTERNAL cursor.getCount() is " + cursor.getCount(), Toast.LENGTH_SHORT).show();
				
				if (cursor.getCount() > 0) {
					cursor.moveToFirst();
					do {
						mTitle = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
						mFilePath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
						mSize = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
						mDate = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED));
						mDuration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

						list.add(new VoiceRecord(context, mTitle, mDate, mDuration, mSize, mFilePath));
						
					} while (cursor.moveToNext());
				}
				
				cursor.close();
				cursor = null;
				
			} else {
				Toast.makeText(context, "INTERNAL cursor is null.", Toast.LENGTH_SHORT).show();
			}
		} catch(Exception e) {

		} finally {
			if(cursor != null)
				cursor.close();
		}		
		
		return list;
	}
	
}
